import cv2
import numpy as np

def is_inside(o,i):
    ox,oy,ow,oh=o
    ix,iy,iw,ih=i
    return ox>ix and oy>iy and ox+ow<ix+iw and oy+oh<iy+ih

def draw_person(image, person):
    """在图像上画一个矩形"""
    x,y,w,h=person
    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,255),2)

img=cv2.imread("misc_pic/people.jpg")
hog=cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

found,w=hog.detectMultiScale(img)

found_fitlered=[]
for ri,r in enumerate(found):
    for qi,q in enumerate(found):
        if ri!=qi and is_inside(r,q):
            break
        else:
            found_fitlered.append(r)

for person in found_fitlered:
    draw_person(img,person)

cv2.imshow("people detection",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
